#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4

'''
Created on Sep 5, 2012

@author: yan
'''

import os
import sys
#from tornado.options import options, define
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi

import django.core.handlers.wsgi

_HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.append(_HERE)
STATIC_ROOT=os.path.join(_HERE,'lottery_terminal','static')
os.environ['DJANGO_SETTINGS_MODULE'] = "lottery_terminal.settings"

#define('port', type=int, default=9000)

def main(port=9000):
    wsgi_app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler())
    tornado_app = tornado.web.Application([
        tornado.web.url(r"/static/(.+)", tornado.web.StaticFileHandler, dict(path=STATIC_ROOT), name='static'),
        ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),])
    server = tornado.httpserver.HTTPServer(tornado_app)
#    server.listen(options.port)
    server.listen(port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == '__main__':
    if len(sys.argv)>1:
        main(int(sys.argv[1]))
    else:
        main()